package com.soft.face.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.soft.face.config.security.TokenStage;
import com.soft.face.entity.User;
import com.soft.face.global.BusinessException;
import com.soft.face.mapper.UserMapper;
import com.soft.face.service.LoginService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class LoginServiceImpl implements LoginService {

    private UserMapper userMapper;

    @Override
    public String login(String username, String password) throws BusinessException {
        // 简单模拟登录
        User user = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUsername, username));
        String md5Password = SecureUtil.md5(password);
        if (user == null) {
            throw new BusinessException("账号不存在");
        }
        if (!user.getPassword().equals(md5Password)) {
            throw new BusinessException("账号或密码不正确");
        }
        // 登录成功返回token
        String token = IdUtil.simpleUUID();
        TokenStage.put( token, username);
        return token;
    }
}
